#!/bin/bash
echo "此脚本用来安装jili debug语音包，安装在/system/app/DuerOSAuto/目录下  @auther 李鹏程"
echo "此脚本用来安装jili debug语音包，安装在/system/app/DuerOSAuto/目录下  @auther 李鹏程"
echo "此脚本用来安装jili debug语音包，安装在/system/app/DuerOSAuto/目录下  @auther 李鹏程"
echo "~/Desktop/workspaces/python-tool/shell/install_others_apk.sh SignedPro_EasyConnect.XG28.7.3.1-18e5d5a3f-202504211933.release.apk EasyConnect"
echo '$1 apk路径；$2 apk的自定义名称'

adb devices
adb root && adb remount && adb shell setenforce 0

# apk的包名
#packagename="com.ecarx.ai.baidurecorder"
# apk路径
originFilepath=$1
if [[ -z $originFilepath ]];then
  read -p "填入安装apk路径:" originFilepath;
  if [[ -z $originFilepath ]];then
    exit
  fi
fi

# apk名称
apkname=$2
if [[ -z $apkname ]];then
  read -p "apk安装命名。假如为空，则默认取文件名为安装名:" apkname;
  if [[ -z $apkname ]];then
    apkname=$originFilepath
    apkname=${apkname##*/}
    apkname=${apkname%.apk}
  fi
fi
echo "apkname:${apkname}"

if [ -f $originFilepath ];then
  echo "${originFilepath} is file"
else
  echo "${originFilepath} is not file; And exit"
  exit
fi

echo "1.install安装"
echo "2.push /system/app/安装+armeabi-v7a"
echo "3.push /system/app/安装+arm64-v8a"
read -p "选择安装方式:" type;

# install安装
if [[ $type -eq 1 ]]; then
	adb install -r -d -t $originFilepath
  echo "install 安装完成"
  exit
fi

# apk名称合法性
if [[ -z $apkname ]];then
  exit
fi

# apk的直接父目录
duerOSAutoDir=${apkname}
rm -rf $duerOSAutoDir
mkdir -p $duerOSAutoDir

# apk电脑上的相对路径
filepath=./$duerOSAutoDir/$apkname.apk
echo "filepath:${filepath}"
cp $originFilepath $filepath

# apk.zip电脑上的相对路径
filepathZip=${filepath}.zip
echo "filepathZip:${filepathZip}"
if [  -f $filepathZip ];then
  rm -rf $filepathZip
  echo "rm ${filepathZip}"
fi

echo "1.clear apk"
array=()
for(( i=0;i<${#array[@]};i++)) do
	echo ${array[i]};
	adb shell pm clear ${array[i]}
	adb uninstall ${array[i]}
	dataAppFilePath=$(adb shell find /data/app/ -name "${array[i]}")
	echo "dataAppFilePath:${dataAppFilePath}"
	adb shell rm -rf $dataAppFilePath
	adb shell rm -rf /data/data/${array[i]}
done;
adb shell rm -rf /system/app/$duerOSAutoDir/
adb shell mkdir -p /system/app/$duerOSAutoDir/

echo "2.push lib"
temp_cache=./$duerOSAutoDir/temp/
cp $filepath $filepathZip
mkdir -p $temp_cache
unzip -o $filepathZip  -d $temp_cache
if [[ $type -eq 2 ]]; then
	rm -rf $temp_cache/lib/arm64-v8a
  mv $temp_cache/lib/armeabi-v7a $temp_cache/lib/arm
else
  rm -rf $temp_cache/lib/armeabi-v7a
  mv $temp_cache/lib/arm64-v8a $temp_cache/lib/arm64
fi
adb push $temp_cache/lib /system/app/$duerOSAutoDir/

echo "3.push apk"
adb push $filepath /system/app/$duerOSAutoDir/

echo "4.clear and reboot"
rm -rf ./$duerOSAutoDir
adb reboot
adb wait-for-device
adb devices
adb root && adb remount && adb shell setenforce 0